
import jwt from 'jsonwebtoken';

const signKey = 'abc';
/**
 * 生成jwt的token
 * @param payload 
 */
export const generateToken = (id: number): Promise<any> => {
  // return jwt.sign({ id }, signKey, { expiresIn: 60 * 60 * 24 * 3 });
  return new Promise((resolve) => {
    const token = jwt.sign({ id }, signKey, { expiresIn: 60 * 60 * 24 * 3 });
    resolve(token);
  })
}

/**
 * 解析出token
 * @param token 
 * @param secret 
 */
export const verifyToken = (token: string): Promise<any> => {
  return new Promise((resolve) => {
    jwt.verify(token, signKey, (error, payload) => {
      if (error) {
        console.log('-----------error start--------------');
        console.log(error);
        console.log('-----------error end--------------');
        // throw new HttpException('token不合法', HttpStatus.OK);
      } else {
        resolve(payload);
      }
    });
  });
}